package com.fhzn.auth.config;

import com.fhzn.auth.advice.ParamValidationErrorResolver;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * @author fhzn
 */
@Slf4j
@EnableConfigurationProperties({SystemProperties.class})
@Configuration
@RequiredArgsConstructor
public class WebMvcConfig implements WebMvcConfigurer {

    @Override
    public void addCorsMappings(CorsRegistry registry) {
        String thirdPartyPath = "/third-party/**";
        registry.addMapping(thirdPartyPath).allowedOrigins("*");
    }

    @ConditionalOnProperty(name = "system.validation.error.present", havingValue = "true")
    @Bean
    public ParamValidationErrorResolver paramValidationErrorResolver() {
        log.info("暴露参数校验异常信息到外部");
        return ParamValidationErrorResolver.RETURN_ACTUAL_ERROR_RESOLVER;
    }

}
